﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;


namespace Evolution
{
    class Mr_Cell
    {
        public const int SizeOfMatrix = 30;
        private Cell[,] MyMatrix;
        class Cell // This is a one cell of big matrix
        {
            public Mr_Cell mr_cell; //parent class
            public int FirstValue = 0;//Value 1 or 0, which will be generated
            public int NearCells = 0;//consist a quantity of nulls in near cells
            public string WhatToDraw = " ";//this string possess the value " " or "*", according tothe parametr FirstValue

            public Cell(int FirstValue, int NearCells, string WhatToDraw, Mr_Cell mr_cell)
            {
                this.mr_cell = mr_cell;
                this.FirstValue = FirstValue;
                this.NearCells = NearCells;
                this.WhatToDraw = WhatToDraw;
            }
        }


        public void GenerateMatrix() //This Method fills MyMatrix the random values such as 0 or 1 
        {
            Random Rand = new Random();
            int Value = 0;
            string ToDraw = " ";
            MyMatrix = new Cell[SizeOfMatrix, SizeOfMatrix];
            for (int i = 0; i < SizeOfMatrix; i++)
                for (int j = 0; j < SizeOfMatrix; j++)
                {
                    Value = Rand.Next(0, 2);
                    if (Value == 1) ToDraw = "*";
                    else ToDraw = " ";
                    MyMatrix[i, j] = new Cell(Value, 0, ToDraw, this);
                }                
        }

        public void TransformMatrix() //This Method transforms MyMatrix according to condition
        {
            for (int i = 0; i < SizeOfMatrix; i++)
                for (int j = 0; j < SizeOfMatrix; j++)
                {
                    CountNearCells(i, j);
                    if ((MyMatrix[i, j].NearCells >= 2) && (MyMatrix[i, j].FirstValue == 0))
                    {
                        MyMatrix[i, j].WhatToDraw = "*";
                    }
                    if ((MyMatrix[i, j].NearCells ==8) && (MyMatrix[i, j].FirstValue == 1))
                    {
                        MyMatrix[i, j].WhatToDraw = " ";
                    }

                }
            for (int i = 0; i < SizeOfMatrix; i++)
                for (int j = 0; j < SizeOfMatrix; j++)
                {
                    if (MyMatrix[i, j].WhatToDraw == "*") MyMatrix[i, j].FirstValue = 1;
                    if (MyMatrix[i, j].WhatToDraw == " ") MyMatrix[i, j].FirstValue = 0;
                }

        }


        public void PrintMatrix()
        {
            int j;
            for (int i = 0; i < SizeOfMatrix; i++)
            {
                for (j = 0; j < SizeOfMatrix; j++)
                {
                    Console.Write(MyMatrix[i, j].WhatToDraw);
                }
                Console.WriteLine();
            }
        }

        private int CountNearCells(int i, int j) // this method counts the "stars" in near cells from a cell[i,j] 
        {
            MyMatrix[i, j].NearCells = 0;
            {
                if ((i - 1) >= 0)
                {
                    if (((j - 1) >= 0) && (MyMatrix[i - 1, j - 1].FirstValue== 1)) { MyMatrix[i, j].NearCells++; }
                    if ((MyMatrix[i - 1, j].FirstValue == 1)) { MyMatrix[i, j].NearCells++; }
                    if (((j + 1) < SizeOfMatrix) && (MyMatrix[i - 1, j + 1].FirstValue == 1)) { MyMatrix[i, j].NearCells++; }
                }
                if ((i + 1) < SizeOfMatrix)
                {
                    if (((j - 1) >= 0) && (MyMatrix[i + 1, j - 1].FirstValue == 1)) { MyMatrix[i, j].NearCells++; }
                    if ((MyMatrix[i + 1, j].FirstValue == 1)) { MyMatrix[i, j].NearCells++; }
                    if (((j + 1) < SizeOfMatrix) && (MyMatrix[i + 1, j + 1].FirstValue == 1)) { MyMatrix[i, j].NearCells++; }
                }
                if (((j - 1) >= 0) && (MyMatrix[i, j - 1].FirstValue == 1)) { MyMatrix[i, j].NearCells++; }
                if (((j + 1) < SizeOfMatrix) && (MyMatrix[i, j + 1].FirstValue == 1)) { MyMatrix[i, j].NearCells++; }
            }
            return MyMatrix[i, j].NearCells; 
        }           
    }

    class Program
    {
        static void Main(string[] args)
        {
            Mr_Cell evolution = new Mr_Cell();
            Console.WriteLine("Project Evolution. Author - Burlay O.I");
            Console.WriteLine("Press Enter to see your initial matrix:");
            Console.ReadLine();
            Console.Clear();
            evolution.GenerateMatrix();
            evolution.PrintMatrix();
            Console.WriteLine();
            Console.WriteLine("If you want to continue, press Space. For exit - press another key");     
            Console.WriteLine();
            Char key = Console.ReadKey().KeyChar;
            while (Convert.ToByte(key) == 32)
            {
                    evolution.TransformMatrix();
                    evolution.PrintMatrix();
                    Console.WriteLine("If you want to continue, press Space. For exit - press another key");   
                    key = Console.ReadKey().KeyChar;
                
            }
            
        }
    }

}
